package com.zkzx.operation.api.service.message;

import com.zkzx.common.constants.ServiceEnum;
import com.zkzx.common.constants.StatusEnum;
import com.zkzx.common.exception.BaseException;
import com.zkzx.common.exception.ExceptionEnum;
import com.zkzx.common.sitemail.command.AnnouncementCommand;
import com.zkzx.common.sitemail.model.AnnouncementVO;
import com.zkzx.operation.api.mapper.AnnouncementMapper;
import com.zkzx.operation.api.mapper.entity.Announcement;
import com.zkzx.operation.api.mapper.entity.AnnouncementExample;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
 * @author young Email:yanghui@zkraising.com
 * @description
 * @company 北京中科智兴物联科技有限公司 @版权所有
 * @since 2021年05月17日 11时07分
 */
@Slf4j
@Service
public class AnnouncementService {

    @Resource
    private AnnouncementMapper announcementMapper;


    /**
     * 获取公告信息
     * @return 公告信息
     */
    public AnnouncementVO get(){
        Announcement announcement = getByCompany();
        if(Objects.isNull(announcement)){
            return null;
        }
        return AnnouncementVO.builder()
                .id(announcement.getId())
                .name(announcement.getName())
                .context(announcement.getContext())
                .status(announcement.getStatus())
                .build();
    }

    /**
     * 获取企业公告
     * @return 企业公告
     */
    private Announcement getByCompany(){
        AnnouncementExample announcementExample = new AnnouncementExample();
        announcementExample.createCriteria().andTypeEqualTo(ServiceEnum.COMPANY.name());
        return announcementMapper.selectByExample(announcementExample).stream().findFirst().orElse(null);
    }


    /**
     * 新增或修改发布公告
     * @param command 内容
     * @param userId 运营人员id
     */
    public void addOrUpdate(AnnouncementCommand command,Integer userId){

        Announcement announcement = new Announcement();
        announcement.setName(command.getName());
        announcement.setType(command.getType());
        announcement.setContext(command.getContext());
        announcement.setStatus(command.getStatus());
        announcement.setLastmodifiedtime(new Date());
        announcement.setLastmodifieduser(userId);

        // 1.当公告不存在就新增
        Announcement announcementOld = getByCompany();
        if(Objects.isNull(announcementOld)){
            announcement.setCreatetime(new Date());
            announcement.setCreateuser(userId);
            announcementMapper.insert(announcement);
            return;
        }

        if(StatusEnum.ENABLE.name().equals(announcementOld.getStatus())){
            throw new BaseException(ExceptionEnum.ANNOUNCEMENT_CODD_ERROR.getCode(),ExceptionEnum.ANNOUNCEMENT_CODD_ERROR.getMsg());
        }

        // 2.否则进行修改
        AnnouncementExample announcementExample = new AnnouncementExample();
        announcementExample.createCriteria().andIdEqualTo(announcementOld.getId());
        announcementMapper.updateByExampleSelective(announcement,announcementExample);

    }


    /**
     * 启用或禁用公告信息
     * @param id 公告id
     * @param statusEnum 状态
     * @param userId 运营人员id
     */
    public void updateStatus(Integer id, StatusEnum statusEnum, Integer userId){
        Announcement announcement = new Announcement();
        announcement.setStatus(statusEnum.name());
        announcement.setLastmodifiedtime(new Date());
        announcement.setLastmodifieduser(userId);

        AnnouncementExample announcementExample = new AnnouncementExample();
        announcementExample.createCriteria().andIdEqualTo(id);

        announcementMapper.updateByExampleSelective(announcement,announcementExample);

    }

}
